// store/user.js
import { defineStore } from "pinia";
import { getUserList } from "@/api/user";
import accessAuth from "@/access/accessAuth";

const useUserStore = defineStore({
	id: "user",
	state: () => ({
		loginUser: {
			userName: "小OJ"
		}
	}),
	actions: {
		// todo 远程请求登录信息
		async getLoginUser() {
			const res = await getUserList();
			if (res.code === 0) {
				this.updateUser(res.data);
			} else {
				this.updateUser({
					...this.loginUser,
					userRole: accessAuth.NOT_LOGIN
				});
			}
		},
		updateUser(payload) {
			this.loginUser = payload;
		},
		clearUser() {
			this.updateUser({
				userName: "小OJ",
				userRole: accessAuth.NOT_LOGIN
			});
		}
	},
	getters: {},
	// 添加持久化配置
	persist: true
});

export default useUserStore;
